package com.bw.module_service.ui.polling

import android.widget.PopupMenu
import android.widget.Toast
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_service.R
import com.bw.module_service.adapter.InspectionAdapter
import com.bw.module_service.databinding.ActivityInspectionBinding
import com.bw.module_service.viewmodel.ServiceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.InspectionActivity)
class InspectionActivity : BaseActivity<ServiceViewModel, ActivityInspectionBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_inspection
    private val mViewModel: ServiceViewModel by viewModels()
    private val inspectionAdapter by lazy { InspectionAdapter() }

    override fun initView() {

        mBinding.back.setOnClickListener {
            finish()
        }



        mViewModel.getPatrolPoints().run {
            mViewModel.inspectionLiveData_.observe(this@InspectionActivity) {
                inspectionAdapter.data.addAll(it.reversed())
                inspectionAdapter.notifyDataSetChanged()
            }
        }


        //模糊查询
        mBinding.editText.setOnClickListener {
            var etInput = mBinding.editText.text.toString()
            if (mBinding.editText.text.length < 0) {
                mViewModel.getPatrolPoints().run {
                    mViewModel.inspectionLiveData_.observe(this@InspectionActivity) {
                        inspectionAdapter.data.clear()
                        inspectionAdapter.data.addAll(it.reversed())
                        inspectionAdapter.notifyDataSetChanged()
                    }
                }
            } else {
                val inputSearch = inspectionAdapter.data.filter { item ->
                    item.pointName.contains(etInput)

                }
                inspectionAdapter.data.clear()
                inspectionAdapter.data.addAll(inputSearch)
                inspectionAdapter.notifyDataSetChanged()
            }


        }
        mBinding.rv.adapter = inspectionAdapter
        mBinding.rv.layoutManager = LinearLayoutManager(this)
        //弹出菜单跳转到新增点位页面
        mBinding.add.setOnClickListener {
            // 创建popMenu页面
            val popupMenu = PopupMenu(this, it)
            // 加载菜单资源
            popupMenu.menuInflater.inflate(R.menu.menu_inspection, popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { menuItem ->
                when (menuItem.itemId) {
                    R.id.menu1 -> {
                        ARouter.getInstance().build(Router.Ui.AddPollingActivity).navigation()
                        true
                    }
                    R.id.menu2 -> {
                        ARouter.getInstance().build(Router.Ui.PatrolInspectionActivity).navigation()
                        true
                    }

                    else -> true
                }
            }
            popupMenu.show() // 正确位置，不参与返回值
        }

        inspectionAdapter.setOnItemClickListener { adapter, view, position ->
            Toast.makeText(this, "详情", Toast.LENGTH_SHORT).show()
          ARouter.getInstance().build(Router.Ui.PollingDetailActivity).withInt("id",inspectionAdapter.data[position].id).navigation()
        }


    }

    override fun onResume() {
        super.onResume()
        inspectionAdapter.data.clear()
        mViewModel.getPatrolPoints()
    }

}